package com.sitecsys.controller;


import com.alibaba.fastjson2.JSONObject;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/api/done")
public class DoneController {

    private final RestTemplate restTemplate;

    public DoneController() {
        this.restTemplate = new RestTemplate();
    }

    @GetMapping("/done_td3_lstm")
    public ResponseEntity<String> sendParamsToDjango() {
        try {

            String djangoApiUrl = "http://localhost:8000/done_td3_lstm/";
            // 将请求体转发给Django
            ResponseEntity<String> responseFromDjango = restTemplate.getForEntity(djangoApiUrl, String.class);

            // 将Django的响应转发给前端
            return ResponseEntity.ok(responseFromDjango.getBody());
        } catch (Exception e) {
            // 处理可能发生的异常
            e.printStackTrace();
            return ResponseEntity.badRequest().body("Error forwarding request to Django");
        }
    }

    @GetMapping("/done_td3_no_lstm")
    public ResponseEntity<String> sendParamsToDjango1() {
        try {

            String djangoApiUrl = "http://localhost:8000/done_td3_no_lstm/";
            // 将请求体转发给Django
            ResponseEntity<String> responseFromDjango = restTemplate.getForEntity(djangoApiUrl, String.class);

            // 将Django的响应转发给前端
            return ResponseEntity.ok(responseFromDjango.getBody());
        } catch (Exception e) {
            // 处理可能发生的异常
            e.printStackTrace();
            return ResponseEntity.badRequest().body("Error forwarding request to Django");
        }
    }

    @GetMapping("/done_sac_r2n2")
    public ResponseEntity<String> sendParamsToDjango2() {
        try {

            String djangoApiUrl = "http://localhost:8000/done_sac_r2n2/";
            // 将请求体转发给Django
            ResponseEntity<String> responseFromDjango = restTemplate.getForEntity(djangoApiUrl, String.class);

            // 将Django的响应转发给前端
            return ResponseEntity.ok(responseFromDjango.getBody());
        } catch (Exception e) {
            // 处理可能发生的异常
            e.printStackTrace();
            return ResponseEntity.badRequest().body("Error forwarding request to Django");
        }
    }

    @GetMapping("/done_fedavg")
    public ResponseEntity<String> sendParamsToDjango3() {
        try {

            String djangoApiUrl = "http://localhost:8000/done_fedavg/";
            // 将请求体转发给Django
            ResponseEntity<String> responseFromDjango = restTemplate.getForEntity(djangoApiUrl, String.class);

            // 将Django的响应转发给前端
            return ResponseEntity.ok(responseFromDjango.getBody());
        } catch (Exception e) {
            // 处理可能发生的异常
            e.printStackTrace();
            return ResponseEntity.badRequest().body("Error forwarding request to Django");
        }
    }


    @GetMapping("/done_fed_aecs")
    public ResponseEntity<String> sendParamsToDjango4() {
        try {

            String djangoApiUrl = "http://localhost:8000/done_fed_aecs/";
            // 将请求体转发给Django
            ResponseEntity<String> responseFromDjango = restTemplate.getForEntity(djangoApiUrl,String.class) ;

            // 将Django的响应转发给前端
            return ResponseEntity.ok(responseFromDjango.getBody());
        } catch (Exception e) {
            // 处理可能发生的异常
            e.printStackTrace();
            return ResponseEntity.badRequest().body("Error forwarding request to Django");
        }
    }


    @PostMapping("/done_1000_td3_no_lstm")
    public JSONObject done_1000_td3_no_lstm(@RequestBody String params) {

        RestTemplate restTemplate = new RestTemplate();
        String djangoApiUrl = "http://localhost:8000/done_1000_td3_no_lstm/";
        JSONObject s = restTemplate.postForObject(djangoApiUrl, params, JSONObject.class);
        return s;
    }

    @PostMapping("/done_1000_td3_lstm")
    public JSONObject done_1000_td3_lstm(@RequestBody String params) {

        RestTemplate restTemplate = new RestTemplate();
        String djangoApiUrl = "http://localhost:8000/done_1000_td3_lstm/";
        JSONObject s = restTemplate.postForObject(djangoApiUrl, params, JSONObject.class);
        return s;
    }

    @PostMapping("/done_1000_sac_r2n2")
    public JSONObject done_1000_sac_r2n2(@RequestBody String params) {

        RestTemplate restTemplate = new RestTemplate();
        String djangoApiUrl = "http://localhost:8000/done_1000_sac_r2n2/";
        JSONObject s = restTemplate.postForObject(djangoApiUrl, params, JSONObject.class);
        return s;
    }

}
